<script setup lang="ts">
import { computed } from 'vue'
import TaskCard from './TaskCard.vue'

const props = defineProps<{
  tasks: any[]
  dateRange: [Date, Date]
}>()

const emit = defineEmits<{
  (e: 'edit-task', task: any): void
}>()

// 处理修改任务
const handleEditTask = (task: any) => {
  emit('edit-task', task)
}

// 按日期分组的任务
const groupedTasks = computed(() => {
  const filtered = props.tasks.filter(task => {
    if (!task.dueDate) return false
    const taskDate = new Date(task.dueDate)
    return (!props.dateRange[0] || !props.dateRange[1] || 
            (taskDate >= props.dateRange[0] && taskDate <= props.dateRange[1]))
  })

  // 按日期分组
  const groups: Record<string, any[]> = {}
  filtered.sort((a, b) => new Date(a.dueDate).getTime() - new Date(b.dueDate).getTime())
  
  filtered.forEach(task => {
    const date = new Date(task.dueDate).toLocaleDateString()
    if (!groups[date]) {
      groups[date] = []
    }
    groups[date].push(task)
  })

  return groups
})
</script>

<template>
  <div class="timeline-container">
    <div 
      v-for="(tasks, date) in groupedTasks" 
      :key="date" 
      class="timeline-group"
    >
      <div class="timeline-date">
        <div class="date-marker"></div>
        <span>{{ date }}</span>
      </div>
      <div class="timeline-tasks">
        <TaskCard
          v-for="task in tasks"
          :key="task.id"
          v-bind="task"
          @edit-task="handleEditTask"
          class="timeline-task"
        />
      </div>
    </div>
  </div>
</template>

<style scoped>
.timeline-container {
  padding: 20px;
  width: 100%;
}

.timeline-group {
  position: relative;
  margin-bottom: 40px;
}

.timeline-date {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  font-size: 16px;
  font-weight: 500;
  color: #303133;
}

.date-marker {
  width: 12px;
  height: 12px;
  background: #409eff;
  border-radius: 50%;
  position: relative;
}

.date-marker::before {
  content: '';
  position: absolute;
  left: 5px;
  top: 12px;
  width: 2px;
  height: calc(100% + 48px);
  background: #e4e7ed;
}

.timeline-group:last-child .date-marker::before {
  display: none;
}

.timeline-tasks {
  margin-left: 28px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.timeline-task {
  margin-bottom: 0;
}
</style> 